package com.zhongen.oa.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.oa.entity.AssetRegistrationEntity;
import com.zhongen.oa.service.AssetRegistrationService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 资产登记
 *
 * @author Code Generator
 * @date 2024-01-22 10:17:31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/assetRegistration")
@Tag(description = "assetRegistration", name = "资产登记管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AssetRegistrationController {

	private final AssetRegistrationService assetRegistrationService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param assetRegistration 资产登记
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_assetRegistration_view')")
	public R getAssetRegistrationPage(@ParameterObject Page page,
			@ParameterObject AssetRegistrationEntity assetRegistration) {
		LambdaQueryWrapper<AssetRegistrationEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(assetRegistration.getAssetNumber()), AssetRegistrationEntity::getAssetNumber,
				assetRegistration.getAssetNumber());
		wrapper.eq(StrUtil.isNotBlank(assetRegistration.getAssetName()), AssetRegistrationEntity::getAssetName,
				assetRegistration.getAssetName());
		wrapper.eq(StrUtil.isNotBlank(assetRegistration.getAssetType()), AssetRegistrationEntity::getAssetType,
				assetRegistration.getAssetType());
		wrapper.eq(Objects.nonNull(assetRegistration.getUserId()), AssetRegistrationEntity::getUserId,
				assetRegistration.getUserId());
		return R.ok(assetRegistrationService.page(page, wrapper));
	}

	/**
	 * 通过id查询资产登记
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_assetRegistration_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(assetRegistrationService.getById(id));
	}

	/**
	 * 新增资产登记
	 * @param assetRegistration 资产登记
	 * @return R
	 */
	@Operation(summary = "新增资产登记", description = "新增资产登记")
	@SysLog("新增资产登记")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_assetRegistration_add')")
	public R save(@RequestBody AssetRegistrationEntity assetRegistration) {
		return R.ok(assetRegistrationService.save(assetRegistration));
	}

	/**
	 * 修改资产登记
	 * @param assetRegistration 资产登记
	 * @return R
	 */
	@Operation(summary = "修改资产登记", description = "修改资产登记")
	@SysLog("修改资产登记")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_assetRegistration_edit')")
	public R updateById(@RequestBody AssetRegistrationEntity assetRegistration) {
		return R.ok(assetRegistrationService.updateById(assetRegistration));
	}

	/**
	 * 通过id删除资产登记
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除资产登记", description = "通过id删除资产登记")
	@SysLog("通过id删除资产登记")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_assetRegistration_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(assetRegistrationService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param assetRegistration 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_assetRegistration_export')")
	public List<AssetRegistrationEntity> export(AssetRegistrationEntity assetRegistration, Long[] ids) {
		return assetRegistrationService.list(Wrappers.lambdaQuery(assetRegistration)
			.in(ArrayUtil.isNotEmpty(ids), AssetRegistrationEntity::getId, ids));
	}

}
